package observer

/**
 * 观察者模式
 * 模拟一个用户订阅公众号的场景
 */
fun main() {
    //定义公众号对象
    val officialAccount = OfficialAccount()

    //消息接收回调接口的实体
    val msgReceived = MsgReceived()

    //用户订阅公众号,并设置消息回调类
    User("zhangSan")
        .subscription(officialAccount)
        .onReceived(msgReceived)

    User("liSi")
        .subscription(officialAccount)
        .onReceived(msgReceived)

    User("zhaoWu")
        .subscription(officialAccount)
        .onReceived(msgReceived)

    //公众号发送一条消息
    officialAccount.notify("这是今天最新的消息！！！")
}

class MsgReceived : OnReceive<String> {
    override fun onUpdateReceived(observer: Observer<String>, msg: String) {
        PrintUtil.print("${(observer as User).userName}:${msg}")
    }
}